import { BufferGeometry, Mesh, Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param attributeName string
 * @param newValue number, string, boolean or object
 * @constructor
 */
class SetGeometryValueCommand<K extends keyof BufferGeometry> extends Command {
	private object:Mesh;
	private attributeName:K;
	private oldValue:any;
	private newValue:any;
	constructor( editor:Editor, object:Object3D, attributeName:K, newValue:any ) {

		super( editor );

		this.type = 'SetGeometryValueCommand';
		this.name = `Set Geometry.${attributeName}`;

		this.object = object as Mesh;
		this.attributeName = attributeName;
		this.oldValue = ( object !== undefined ) ? this.object.geometry[ attributeName ] : undefined;
		this.newValue = newValue;

	}

	execute() {

		this.object.geometry[ this.attributeName ] = this.newValue;
		this.editor.signals.objectChanged.dispatch( this.object );
		this.editor.signals.geometryChanged.dispatch();
		this.editor.signals.sceneGraphChanged.dispatch();

	}

	undo() {

		this.object.geometry[ this.attributeName ] = this.oldValue;
		this.editor.signals.objectChanged.dispatch( this.object );
		this.editor.signals.geometryChanged.dispatch();
		this.editor.signals.sceneGraphChanged.dispatch();

	}

}

export { SetGeometryValueCommand };
